﻿using LView.CMS.Core.Helper;
using LView.CMS.IServices;
using LView.CMS.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace LView.CMS.Site.Controllers
{
    public class TaskController : BaseController
    {
        private readonly ITaskService _taskService;
        private readonly IManagerRoleService _roleservice;
        public TaskController(IManagerRoleService roleservice, ITaskService taskService)
        {
            _roleservice = roleservice;
            _taskService = taskService;
        }

        public IActionResult Index()
        {
            return View();
        }

        public async Task<string> LoadData([FromQuery] PageModel model)
        {
            var result = await _taskService.LoadDataAsync(model);
            return JsonHelper.ObjectToJSON(result);
        }

        public IActionResult AddOrModify()
        {
            return View();
        }

        public IActionResult SendTask()
        {
            var roleList = _roleservice.GetListByCondition(new ManagerRoleRequestModel
            {
                Key = null
            });
            ViewData["VideoUrl"] = string.Format("~/LView.CMS.Assets/Video/{0}", "20191210各环节缓存Cache解析.mp4");
            return View(roleList);
        }

        [HttpPost]
        [Route("/Task/AddOrModify")]
        public async Task<string> AddOrModifyAsync([FromForm] TaskAddOrModifyModel model)
        {
            var result = new BaseResult();
            model.CreateUserId = CurrentUser.UserId;
            model.CreateUserName = CurrentUser.UserName;
            model.DIR = CurrentUser.UserName;
            result = await _taskService.AddOrModifyAsync(model);
            return JsonHelper.ObjectToJSON(result);
        }

        [HttpPost]
        public async Task<string> Delete(string[] taskId)
        {
            var result = await _taskService.DeleteIdsAsync(taskId);
            return JsonHelper.ObjectToJSON(result);
        }

        [HttpPost]
        public async Task<string> SendTaskAsync(string Id, string taskName, string RoleId)
        {
            var result = await _taskService.SendTaskAsync(Id, taskName, CurrentUser.UserName, RoleId);
            return JsonHelper.ObjectToJSON(result);
        }
    }
}
